$(document).ready(function () {
    var nowMenuIndex = null
    let width = document.body.clientWidth
    // 轮播图初始化
    if (location.pathname == '/') {
        var swiper = new Swiper(".mySwiper", {
            spaceBetween: 30,
            autoplay: true,
            effect: "fade",
            navigation: {
                nextEl: ".swiper-button-next",
                prevEl: ".swiper-button-prev",
            },
            pagination: {
                el: ".swiper-pagination",
                clickable: true,
            },
        });
    }
    // 菜单选择状态
    function editActive(node, status, index) {

        $('.menu-item').each(function () {
            let id = $(this).data('key') * 1

            if (id == nowMenuIndex) {
                // 当前页面的nav不用取消
                return
            }
            $(this).removeClass('menu-active')
        })

        if (!status) {
            $(node).addClass('menu-active')
        }
        //配置菜单下拉显示
        $('.memu-pop-content').each(function () {
            $(this).hide()
        })
        if (index) {
            $('.memu-pop-content').eq(index - 1).show()
            $('.memu-pop-content').eq(index - 1).addClass('memu-pop-content-into')
        }
    }
    function closeMenu(_this) {
        $(_this).removeClass('memu-pop-content-into')
        $(_this).addClass('memu-pop-content-back')
        setTimeout(() => {
            $(_this).removeClass('memu-pop-content-back')
            $('.menu-pop').hide();
            editActive(null, true, null)
        }, 200);
    }
    //下拉弹出事件处理
    $('.menu-item').each(function () {
        $(this).on('mouseenter', function () {
            // 关闭搜索框
            $('.search-box').removeClass('memu-pop-content-into').hide()

            let key = $(this).data('key') * 1
            if (key > 3) {
                $('.memu-pop-content-into').each(function () {
                    closeMenu(this)
                })
                return
            }
            editActive(this, false, key)
            if ($('.menu-pop').is(':visible')) {
                return
            }
            $('.menu-pop').toggle();
        });
    })
    $('.memu-pop-content').on('mouseleave', function (e) {
        e.stopPropagation()
        closeMenu(this)
    })
    $('.menu-more').on('click', function () {
        $('.platform-nav').hide()
        $('.mask').show();
        $(".mask-h5-menu-box").show()
        $('.mask-list-frist').addClass('menu-into')
    });
    // 下拉弹出事件-绑定H5
    $(".mask-h5-menu .mask-left").on('click', function () {
        $('.mask-left i').hide()
        $('.mask-right i').show()
        $('.mask-list-sec').each(function () {
            $(this).removeClass('menu-out')
            $(this).addClass('menu-out')
            setTimeout(() => {
                $(this).hide()
                $(this).removeClass('menu-out')
            }, 300);
        })
    })

    $(".mask-h5-menu .mask-right").on('click', function () {
        $('.platform-nav').show()
        $('.mask-list-frist').removeClass('menu-into')
        $('.mask-list-frist').addClass('menu-out')
        setTimeout(() => {
            $('.mask-list-frist').removeClass('menu-out')
            $(".mask-h5-menu-box").hide()
            $('.mask').hide();
        }, 300);
    })

    $('.mask-frist-item').each(function () {
        let _this = this
        $(_this).on('click', function () {
            let key = $(this).data('key') * 1 - 1
            $('.mask-list-sec').eq(key).show()
            $('.mask-list-sec').eq(key).addClass('menu-into')
            $('.mask-left i').show()
            $('.mask-right i').hide()
        });
    })
    // 搜索功能绑定
    $('.btn-search').on('click', function () {
        if ($('.memu-pop-content-into').length) {
            closeMenu($('.memu-pop-content-into'))
        }
        $('.search-box').addClass('memu-pop-content-into').show()
        $('.search-input input').focus()
    })
    $('.search-close').on('click', function () {
        $('.search-box').removeClass('memu-pop-content-into').addClass('memu-pop-content-back')
        setTimeout(() => {
            $('.search-box').removeClass('memu-pop-content-back').hide()
        }, 300);
    })
    // 更新当前选中项
    function updateNowNav() {
        if(location.pathname === '/'){
            var nowPath = null;
        }else{
            if(location.pathname.match(/\/([a-zA-Z]+)-/)){
                var nowPath = location.pathname.match(/\/([a-zA-Z]+)-/)[1]
            }else if(location.pathname.match(/\/([a-zA-Z]+)\//)){
                var nowPath = location.pathname.match(/\/([a-zA-Z]+)\//)[1]
            }else{
                var nowPath = null;
            }
        }
        console.log(nowPath);
        let pathList = [['cloud'], ['cdn'], ['partners'], ['help'], ['news'], ['contact', 'about']]
        let nowIndex = pathList.findIndex((item) => item.includes(nowPath))
        $('.header-box').css('opacity', 1)
        if (nowIndex < 0) {
            return
        }
        $(width > 1440 ? '.menu-item' : '.mask-frist-item').eq(nowIndex).addClass('menu-active')
        nowMenuIndex = nowIndex !== 5 ? nowIndex + 1 : 3
    }
    updateNowNav()

    // 绑定搜索框输入事件
    $('.search-input input').on('keydown', function (e) {
        $(this).removeClass('error-input')
        if (e.key == 'Enter') {
            let val = $(this).val().trim()
            if (val) {
                window.location.href = '../../search/?word=' + val
            } else {
                $(this).addClass('error-input').focus()
            }
        }
    })

    $('.search-input img').on('click', function (e) {
        let val = $('.search-input input').val().trim()
        if (val) {
            window.location.href = '../../search/?word=' + val
        } else {
            $('.search-input input').addClass('error-input').focus()
        }
    })
})